FROM php:7.2.34-fpm-alpine3.12


# 时区设置
ENV TIMEZONE Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TIMEZONE /etc/localtime && echo $TIMEZONE > /etc/timezone

# 查询 alpine 版本 && 设置镜像源
RUN source /etc/os-release && printf "\
http://mirrors.aliyun.com/alpine/v${VERSION_ID%.*}/main\n\
http://mirrors.aliyun.com/alpine/v${VERSION_ID%.*}/community\n\
" > /etc/apk/repositories

# re2c PHP词法解析器
ENV PHPIZE_DEPS autoconf g++ gcc make file re2c linux-headers

# 更新最新本地镜像源
RUN apk update

#创建运行账号,指定uid与pid
RUN addgroup -S nginx -g 1001 \
    && adduser -D -S -h /var/cache/nginx -s /sbin/nologin -u 1001 -G nginx nginx
# 安装基础库
RUN apk add openssl

RUN apk add --no-cache --virtual .build-deps $PHPIZE_DEPS \
\
# PHP GD
&& apk add --no-cache libpng libjpeg-turbo freetype libxpm  libstdc++\
&& apk add --no-cache --virtual .build-tmp libpng-dev libjpeg-turbo-dev libxpm-dev freetype-dev \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-xpm-dir=/usr/include/ \
&& docker-php-ext-install gd \
\
# PHP mysqli
&& docker-php-ext-install mysqli \
\
# PHP pdo_mysql
&& docker-php-ext-configure pdo_mysql --with-pdo-mysql=mysqlnd \
&& docker-php-ext-install pdo_mysql \
\
# PHP bcmath
&& docker-php-ext-install bcmath \
\
# PHP opcache
&& docker-php-ext-install opcache \
# PHP sockets
&& docker-php-ext-install sockets \
# PHP mbstring
&& apk add --no-cache oniguruma \
&& apk add --no-cache --virtual .build-tmp oniguruma-dev \
&& docker-php-ext-install mbstring \
# PHP zip
&& apk add --no-cache libzip \
&& apk add --no-cache --virtual .build-tmp libzip-dev \
&& docker-php-ext-install zip \
#其它扩展
&& apk add --no-cache libxml2-dev gettext-dev libxslt-dev icu-dev \
&& apk add --no-cache --virtual .build-tmp libzip-dev \
&& docker-php-ext-install xmlrpc \
&& docker-php-ext-install exif \
&& docker-php-ext-install gettext \
&& docker-php-ext-install intl \
&& docker-php-ext-install pcntl \
&& docker-php-ext-install soap \
&& docker-php-ext-install sysvsem \
&& docker-php-ext-install xsl


#mcrypt
RUN apk add --no-cache libmcrypt-dev \
&& apk add --no-cache --virtual .build-tmp \
&&  pecl install  mcrypt-1.0.3 && docker-php-ext-enable mcrypt

# swoole
RUN pecl install swoole-4.6.6 && docker-php-ext-enable swoole

# protobuf
RUN pecl install protobuf-3.15.8 && docker-php-ext-enable  protobuf
# PHP redis
RUN pecl install redis-5.3.1  && docker-php-ext-enable redis

# PHP amqp 扩展
RUN apk add --no-cache rabbitmq-c \
&& apk add --no-cache --virtual .build-tmp rabbitmq-c-dev \
&& pecl install amqp \
&& docker-php-ext-enable amqp

# composer
RUN curl -sS https://getcomposer.org/installer | php \
    && mv composer.phar /usr/bin/composer
# use aliyun composer
RUN composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/

# 安装 yaconf
RUN pecl install yaconf && docker-php-ext-enable yaconf

# 安装 yar
RUN apk add curl-dev \
&& pecl install msgpack && docker-php-ext-enable msgpack \
&& pecl install https://pecl.php.net/get/yar-2.0.4.tgz && docker-php-ext-enable yar

# 安装 yaf, 版本太高会导致类无法自动加载
RUN pecl install https://pecl.php.net/get/yaf-3.0.9.tgz && docker-php-ext-enable yaf

# grpc
RUN   pecl channel-update pecl.php.net && pecl install https://pecl.php.net/get/grpc-1.30.0.tgz && docker-php-ext-enable grpc

# mongodb
RUN pecl install https://pecl.php.net/get/mongodb-1.8.2.tgz && docker-php-ext-enable mongodb

# 清除构建包，减少镜像体积
RUN apk del .build-deps .build-tmp

#tenginx install
ENV TENGINE_VERSION 2.3.1

RUN rm -rf /var/cache/apk/* && \
    rm -rf /tmp/*
ENV CONFIG "\
        --prefix=/etc/nginx \
        --sbin-path=/usr/sbin/nginx \
        --modules-path=/usr/lib/nginx/modules \
        --conf-path=/etc/nginx/nginx.conf \
        --error-log-path=/var/log/nginx/error.log \
        --http-log-path=/var/log/nginx/access.log \
        --pid-path=/var/run/nginx.pid \
        --lock-path=/var/run/nginx.lock \
        --http-client-body-temp-path=/var/cache/nginx/client_temp \
        --http-proxy-temp-path=/var/cache/nginx/proxy_temp \
        --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
        --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
        --http-scgi-temp-path=/var/cache/nginx/scgi_temp \
        --user=nginx \
        --group=nginx \
        --with-http_ssl_module \
        --with-http_realip_module \
        --with-http_addition_module \
        --with-http_sub_module \
        --with-http_dav_module \
        --with-http_flv_module \
        --with-http_mp4_module \
        --with-http_gunzip_module \
        --with-http_gzip_static_module \
        --with-http_random_index_module \
        --with-http_secure_link_module \
        --with-http_stub_status_module \
        --with-http_auth_request_module \
        --with-http_xslt_module=dynamic \
        --with-http_image_filter_module=dynamic \
        --with-http_geoip_module=dynamic \
        --with-threads \
        --with-stream \
        --with-stream_ssl_module \
        --with-stream_ssl_preread_module \
        --with-stream_realip_module \
        --with-stream_geoip_module=dynamic \
        --with-http_slice_module \
        --with-mail \
        --with-mail_ssl_module \
        --with-compat \
        --with-pcre-jit \
        --with-http_v2_module \
        --add-module=modules/ngx_http_upstream_check_module \
        --add-module=modules/ngx_http_proxy_connect_module \
        --add-module=modules/ngx_http_user_agent_module \
        --add-module=modules/ngx_http_upstream_session_sticky_module \
        --add-module=modules/ngx_http_slice_module \
        --add-module=modules/ngx_http_reqstat_module \
        --add-module=modules/ngx_http_footer_filter_module \
        --add-module=modules/ngx_http_concat_module \
        --add-module=modules/ngx_http_trim_filter_module \
        --add-module=modules/nginx-module-vts \
        "
RUN     apk add --no-cache --virtual .build-deps \
        gcc \
        g++ \
        libc-dev \
        make \
        openssl-dev \
        pcre-dev \
        zlib-dev \
        linux-headers \
        curl \
        libxslt-dev \
        gd-dev \
        geoip-dev \
        && curl -L "https://tengine.taobao.org/download/tengine-${TENGINE_VERSION}.tar.gz" -o tengine.tar.gz \
        && mkdir -p /usr/src \
        && tar -zxC /usr/src/ -f tengine.tar.gz \
        && rm tengine.tar.gz \
        && wget https://github.com/vozlt/nginx-module-vts/archive/refs/tags/v0.1.16.tar.gz \
        && tar xf v0.1.16.tar.gz \
        && rm -f v0.1.16.tar.gz \
        && mv nginx-module-vts-0.1.16  /usr/src/tengine-${TENGINE_VERSION}/modules/nginx-module-vts \
        && cd /usr/src/tengine-${TENGINE_VERSION} \
        && ls -l /usr/src/tengine-$TENGINE_VERSION/modules \
        && ./configure $CONFIG --with-debug \
        && make -j$(getconf _NPROCESSORS_ONLN) \
        && mv objs/nginx objs/nginx-debug \
        && mv objs/ngx_http_xslt_filter_module.so objs/ngx_http_xslt_filter_module-debug.so \
        && mv objs/ngx_http_image_filter_module.so objs/ngx_http_image_filter_module-debug.so \
        && mv objs/ngx_http_geoip_module.so objs/ngx_http_geoip_module-debug.so \
        && mv objs/ngx_stream_geoip_module.so objs/ngx_stream_geoip_module-debug.so \
        && ./configure $CONFIG \
        && make -j$(getconf _NPROCESSORS_ONLN) \
        && make install \
        && rm -rf /etc/nginx/html/ \
        && mkdir /etc/nginx/conf.d/ \
        && mkdir -p /usr/share/nginx/html/ \
        && install -m644 html/index.html /usr/share/nginx/html/ \
        && install -m644 html/50x.html /usr/share/nginx/html/ \
        && install -m755 objs/nginx-debug /usr/sbin/nginx-debug \
        && install -m755 objs/ngx_http_xslt_filter_module-debug.so /usr/lib/nginx/modules/ngx_http_xslt_filter_module-debug.so \
        && install -m755 objs/ngx_http_image_filter_module-debug.so /usr/lib/nginx/modules/ngx_http_image_filter_module-debug.so \
        && install -m755 objs/ngx_http_geoip_module-debug.so /usr/lib/nginx/modules/ngx_http_geoip_module-debug.so \
        && install -m755 objs/ngx_stream_geoip_module-debug.so /usr/lib/nginx/modules/ngx_stream_geoip_module-debug.so \
        && ln -s ../../usr/lib/nginx/modules /etc/nginx/modules \
        && strip /usr/sbin/nginx* \
        && strip /usr/lib/nginx/modules/*.so \
        && rm -rf /usr/src/tengine-$NGINX_VERSION \
        && apk add --no-cache --virtual .gettext gettext \
        && mv /usr/bin/envsubst /tmp/ \
        && runDeps="$( \
                scanelf --needed --nobanner --format '%n#p' /usr/sbin/nginx /usr/lib/nginx/modules/*.so /tmp/envsubst \
                        | tr ',' '\n' \
                        | sort -u \
                        | awk 'system("[ -e /usr/local/lib/" $1 " ]") == 0 { next } { print "so:" $1 }' \
        )" \
        && apk add --no-cache --virtual .nginx-rundeps $runDeps \
        && apk del .build-deps \
        && apk del .gettext \
        && mv /tmp/envsubst /usr/local/bin/ \
        && apk add --no-cache tzdata

#entrypoint.sh
RUN echo "#!/bin/sh " >> /usr/bin/nginx-php-entrypoint.sh  \
    && echo  "nginx " >> /usr/bin/nginx-php-entrypoint.sh  \
    && echo "exec php-fpm" >> /usr/bin/nginx-php-entrypoint.sh  \
    && chmod +x /usr/bin/nginx-php-entrypoint.sh

# 设置工作目录
RUN mkdir /data
WORKDIR /data

CMD ["/usr/bin/nginx-php-entrypoint.sh"]